<?php

class Mycommon
{

    function getCurrentUrl()
    {
        $pageURL = 'http';
        if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        $pageURL .= $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
        return $pageURL;
    }

    function isUrl($url)
    {
        return (preg_match('/^(http|https):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? true : false;
    }

    function getListPaging($cPage, $pCount)
    {
        $listPaging    = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        $startShowPage = 0;
        $startShowPage = ($pCount > 5) ? (($cPage > 3) ? $cPage - 2 : 1) : 1;
        if (($cPage + 2) > $pCount && $pCount > 5) {
            $startShowPage -= ($cPage + 2) - $pCount;
        }
        $index = 0;
        $i     = 0;
        for ($i = 0; $i < 5; $i++) {
            if (($startShowPage + $i) <= $pCount) {
                $listPaging[$index] = $startShowPage + $i;
                $index++;
            } else {
                break;
            }
        }
        return $listPaging;
    }

    function getDomainFromUrl($strUrl)
    {
        $strUrl = trim($strUrl);
        if (trim($strUrl) == '')
            return '';
        $parse  = parse_url($strUrl);
        $rel    = '';
        if (isset($parse['host'])) {
            $host = mb_strtolower($parse['host']);
            $pos  = strpos($host, 'www.');
            if ($pos !== false) {
                $rel = substr($host, $pos + 4);
            } else {
                $rel = $host;
            }
        }
        return $rel;
    }

}

function debug($value, $label = null)
{
    $label = get_tracelog(debug_backtrace(), $label);
    echo getdebug($value, $label);
    exit();
}

function getdebug($value, $label = null)
{
    $value = htmlentities(print_r($value, true));
    return "<pre>$label$value</pre>";
}

function get_tracelog($trace, $label = null)
{
    $line  = $trace[0]['line'];
    $file  = is_set($trace[1]['file']);
    $func  = $trace[1]['function'];
    $class = is_set($trace[1]['class']);
    $log   = "<span style='color:#FF3300'>-- $file - line:$line - $class-$func()</span><br/>";
    if ($label)
        $log .= "<span style='color:#FF99CC'>$label</span> ";
    return $log;
}

function is_set(&$var, $substitute = null)
{
    return isset($var) ? $var : $substitute;
}

function dump($value, $label = null)
{
    $label = get_tracelog(debug_backtrace(), $label);
    $value = htmlentities(var_export($value, true));
    echo "<pre>$label$value</pre>";
}
